<?php
global $_W, $_GPC;
$openid = m('user')->getOpenid();
$member = m('member')->getMember($openid);
$op = empty($_GPC['op']) ? 'display' : $_GPC['op'];
if($op == "display"){
    if($member['isShop']!=1){
     exit;
    }
    if($member['shoptype']==2 || $member['shoptype']==3){
    $list=pdo_fetchall('select * from '.tablename('ewei_shop_commission_editor_article').' where uniacid=:uniacid and openid=:openid order by id desc ',array(':uniacid'=>$_W['uniacid'],':openid'=>$openid));
    foreach($list as $k=>$v){
     $list[$k]['article_img']="http://wx.lanxuevip.com/attachment/".$v['article_img'];
     $list[$k]['createtime']=date('Y-m-d H:i:s',$v['createtime']);
    }
    include $this->template('editorarticle_list');
    }
}
if($op == "editor"){
    if($member['isShop']!=1){
     exit;
    }
    if($member['shoptype']==2 || $member['shoptype']==3){
        //编辑文章
        $id=$_GPC['id'];
        if(!empty($id)){
        $info=pdo_fetch('select * from '.tablename('ewei_shop_commission_editor_article')." where uniacid=:uniacid and id=:id ",array(':uniacid'=>$_W['uniacid'],':id'=>$id));
            if(!empty($info)){
            $info['image']="http://wx.lanxuevip.com/attachment/".$info['article_img'];
            $info['article_content']=htmlspecialchars_decode($info['article_content']);
            }
        }
        if($_GPC['data']){
            $data['article_img']=$_GPC['data']['article_img'];
            $data['article_title']=$_GPC['data']['article_title'];
            $data['article_content']=$_GPC['data']['article_content'];
            $data['uniacid']=$_W['uniacid'];
            $data['openid']=$openid;
            $data['createtime']=time();
            if(!empty($_GPC['data']['article_id'])){
                //更新文章
                $article_info=pdo_fetch('select * from '.tablename('ewei_shop_commission_editor_article')." where uniacid=:uniacid and id=:id ",array(':uniacid'=>$_W['uniacid'],':id'=>$_GPC['data']['article_id']));
                //判断是否存在这篇文章和是否审核
                if(!empty($article_info) && $article_info['status']==0){
                pdo_update('ewei_shop_commission_editor_article',$data,array('id'=>$_GPC['data']['article_id'],'uniacid'=>$_W['uniacid']));
                show_json(1,array('article_id'=>$_GPC['data']['article_id']));
                }else{
                show_json(0);  
                }
            }else{
                pdo_insert('ewei_shop_commission_editor_article', $data);
                $article_id = pdo_insertid();
                if($article_id){
                show_json(1,array('article_id'=>$article_id));
                }else{
                show_json(0);  
                }
            }
        }
  include $this->template('editorarticle_editor');  
    }
}
if($op == "detail"){
  $id=$_GPC['id'];
  $mid=$_GET['mid'];
  if(empty($id)){
     exit; 
  }
  $info=pdo_fetch('select * from '.tablename('ewei_shop_commission_editor_article')." where uniacid=:uniacid and id=:id ",array(':uniacid'=>$_W['uniacid'],':id'=>$id));
  if(empty($info)){
  echo "该文章已删除";exit; 
  }
        /*
      *  gwj
      *  修改：会员发展会员
      *  时间：2016年10月8日
      */
     if($mid != $member['id'] && !empty($mid)){
             if(!$member['agentid']){
                     //更新为下级
                     $parent=pdo_fetch('select * from'.tablename('ewei_shop_member')."where id='{$mid}' and uniacid='{$_W['uniacid']}'");
                     $set=p('commission')->getSet();
                     $tm = $set['tm'];
                     $templateid = $tm['templateid'];
                     $message = $tm['commission_agent_new'];
                     $msg = array('keyword1' => array('value' => !empty($tm['commission_agent_newtitle']) ? $tm['commission_agent_newtitle'] : '新增下线通知', "color" => "#73a68d"), 'keyword2' => array('value' => $message, "color" => "#73a68d"));
                     if (!empty($templateid)) {
                              m('message')->sendTplNotice($parent['openid'], $templateid, $msg);
                      } else {
                              m('message')->sendCustomNotice($parent['openid'], $msg);
                      }
                      pdo_query('update ' . tablename('ewei_shop_member') . " set agentid='{$mid}' where id=:id and uniacid='{$_W['uniacid']}' ", array(":id" => $member['id']));
             }
     }
  $authorinfo=pdo_fetch('select * from '.tablename('ewei_shop_member')." where uniacid=:uniacid and openid=:openid ",array(':uniacid'=>$_W['uniacid'],':openid'=>$info['openid']));
  $info['article_content']=htmlspecialchars_decode($info['article_content']);
  $info['createtime']=date('Y-m-d H-i-s',$info['createtime']);
  $info['author']=$authorinfo['nickname'];
  $info['author_id']=$authorinfo['id'];
  $info['image']="http://wx.lanxuevip.com/attachment/".$info['article_img'];
  include $this->template('editorarticle_detail'); 
}
if($op == "del"){
  $id=$_GPC['id'];
  if(empty($id)){
     exit; 
  }
  $info=pdo_fetch('select * from '.tablename('ewei_shop_commission_editor_article')." where uniacid=:uniacid and id=:id ",array(':uniacid'=>$_W['uniacid'],':id'=>$id));
  if(!empty($info)){
  pdo_delete("ewei_shop_commission_editor_article", array('uniacid'=>$_W['uniacid'],'id'=>$id));
  }
  $redirect=$this->createPluginMobileUrl('commission/editor_article',array('op'=>'editor'));
  header('location: '.$redirect);
}